//
//  HXTViewController.m
//  掌上宏力
//
//  Created by admin on 2017/4/26.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "HXTViewController.h"
#import "HXTCardInfoVC.h"
#import "PersonalFileModel.h"
#import "HXTCardBalanceVC.h"
#import "HXTFuLiInfoVC.h"
#import "HXTConsumInfoVC.h"
#import "CardBalanceModel.h"
#import "HXTFuLiConsumVC.h"
#import "HXTListViewModel.h"
#import "BSStaticTBCell.h"


NSString *const hxtBlance_url = @"api/Hxt/HxtInfocardYueJf?";

@interface HXTViewController ()
@property (nonatomic,strong) HXTListViewModel *viewModel;
@property (nonatomic,strong) UIView *hideView;
@property (nonatomic,strong) UIAlertController *touchIdHUD;

@end

@implementation HXTViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"宏信通";
    
    [self.view addSubview:_hideView];
}

-(void)getDatasArray{
    [self getDatasArray:^(GetDatas handler) {
        _viewModel = [[HXTListViewModel alloc] init];
        [_viewModel getHXTListArray:^{
            handler(_viewModel.datasArray);
        }];
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _viewModel.datasArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 4) {
        return 15.0;
    }
    return 5;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.section) {
        case 0:{
            HXTCardInfoVC *cardVC = [[HXTCardInfoVC alloc] init];
            [self pushViewControllerTo:cardVC];
        }
            break;
        case 1:{
            
            [self pushToCardBalanceView];
            
        }
            break;
        case 2:{
            HXTConsumInfoVC *consumVC = [[HXTConsumInfoVC alloc] init];
            [self pushViewControllerTo:consumVC];
            
        }
            break;
        case 3:{
            HXTFuLiInfoVC *fuliVC = [[HXTFuLiInfoVC alloc] init];
            [self pushViewControllerTo:fuliVC];
        }
            break;
            
        default:{
            HXTFuLiConsumVC *flConsumV = [[HXTFuLiConsumVC alloc] init];
            [self pushViewControllerTo:flConsumV];
        }
            break;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BSStaticTBCell *cell = [BSStaticTBCell cellWithTableView:tableView];
    cell.title = _viewModel.datasArray[indexPath.section];
    
    return cell;
}
//跳转到积分余额信息页面
-(void)pushToCardBalanceView{
    User *user = [User currentUser];
    
    NSString *hxtPWD = [NetWorker sharedNetWorker].hxtPWD;
    if (hxtPWD) {
        NSString *url = [NSString stringWithFormat:@"%@%@HxtCardNo=%@&CardPwd=%@",HOSPITAL_URL,hxtBlance_url,user.MedicalCard,hxtPWD];
        [NetWorker getHXTDatasWithURLStr:url success:^(id result) {
            if ([result[@"status"] isEqualToString:@"success"]) {
                HXTCardBalanceVC *cardBlanceVC = [[HXTCardBalanceVC alloc] init];
                //                cardBlanceVC.hxtPwd = pwdT.text;
                cardBlanceVC.cardBalance = [CardBalanceModel yy_modelWithJSON:result[@"result"]];
                [self pushViewControllerTo:cardBlanceVC];
                _hideView.hidden = YES;
            }
            
        } failure:^(id result) {
            [SVProgressHUD showSuccessWithStatus:@"获取信息失败"];
        }];
        
    }else{
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"输入密码" message:@"第一次查看需要输入宏信通密码" preferredStyle:UIAlertControllerStyleAlert];
        [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            UITextField *pwdT = alertC.textFields.firstObject;
            //先访问网络
            NSString *url = [NSString stringWithFormat:@"%@%@HxtCardNo=%@&CardPwd=%@",HOSPITAL_URL,hxtBlance_url,user.MedicalCard,pwdT.text];
            [NetWorker getHXTDatasWithURLStr:url success:^(id result) {
                if ([result[@"status"] isEqualToString:@"success"]) {
                    HXTCardBalanceVC *cardBlanceVC = [[HXTCardBalanceVC alloc] init];
                    [NetWorker sharedNetWorker].hxtPWD = pwdT.text;
                    cardBlanceVC.cardBalance = [CardBalanceModel yy_modelWithJSON:result[@"result"]];
                    [self pushViewControllerTo:cardBlanceVC];
                    _hideView.hidden = YES;
                }
                
            } failure:^(id result) {
                UITextField *pwdT = alertC.textFields.firstObject;
                pwdT.text = @"";
                alertC.title = @"密码验证失败";
                alertC.message = @"请输入正确的宏信通密码";
                [self presentViewController:alertC animated:YES completion:nil];
                
            }];
            
        }]];
        [alertC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            _hideView.hidden = YES;
        }]];
        [alertC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            textField.secureTextEntry = YES;
            textField.keyboardType = UIKeyboardTypeNumberPad;
        }];
        [self presentViewController:alertC animated:YES completion:nil];
    }
}

@end
